package ma.myschool.app.model;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import ma.myschool.app.Constants.GenreEleve;
import ma.myschool.app.util.CommonUtils;

@Entity
@Table(name = "eleve")
public class Eleve extends BaseObject {

	private static final long serialVersionUID = 6696443542727618987L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "eleve_id")
	private Long id;
	
	@Column(name = "ordre_classe")
	private int ordreClasse;
	
	@Column(name = "genre")
	private GenreEleve genre;
	
	@Column(name = "numero_insciption", length = 15)
	private String numeroInscription;
	
	@Column(name = "numero_insciption_national", length = 30)
	private String numeroInscriptionNational;
	
	@Column(name = "date_inscription")
	private Date dateInscription = Calendar.getInstance().getTime();
	
	@Column(name = "date_depart")
	private Date dateDepart;

	@Column(name = "nom", length = 50)
	private String nom;
	
	@Column(name = "nom_ar", length = 50)
	private String nomAr;

	@Column(name = "prenom", length = 50)
	private String prenom;
	
	@Column(name = "prenom_ar", length = 50)
	private String prenomAr;
	
	@Transient
	private String imageToDisplay;
	
	@Column(name = "date_naissance")
	private Date dateNaissance;
	
	@Column(name = "image")
	private String image;
	
	@Column(name = "lieu_naissance", length = 50)
	private String lieuNaissance;
	
	@Column(name = "lieu_naissance_ar", length = 50)
	private String lieuNaissanceAr;
	
	@Column(name = "adresse", length = 150)
	private String adresse;
	
	@Column(name = "adresse_ar", length = 150)
	private String adresseAr;

	@Column(name = "telephone_parent", length = 15)
	private String telephoneParent;
	
	@Column(name = "telephone_mere_1", length = 15)
	private String telephoneMere1;
	
	@Column(name = "telephone_mere_2", length = 15)
	private String telephoneMere2;
	
	@Column(name = "telephone_pere_1", length = 15)
	private String telephonePere1;
	
	@Column(name = "telephone_pere_2", length = 15)
	private String telephonePere2;
	
	@Column(name = "email_parent", length = 50)
	private String emailParent;
	
	@Column(name = "email_eleve", length = 50)
	private String emailEleve;

	@Column(name = "paiement_mensuel")
	private float paiementMensuel;
	
	@Column(name = "paiement_transport")
	private float paiementTransport;
	
	@Column(name = "deleted")
	private boolean deleted;
	
	@Column(name = "is_exonere")
	private boolean exonere;
	
	@Transient
	private List<MoisPaye> moisAPayes; 
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="groupe_id", nullable = false)
	private Groupe groupe = new Groupe();
	
	
	public Eleve() {
		super();
	}

	public Eleve(Long id, int ordreClasse, String numeroInscription,
			Date dateInscription, String nom, String prenom,
			Date dateNaissance, String lieuNaissance, String adresse,
			String telephoneParent, String emailParent, String emailEleve,
			float paiementMensuel, float paiementTransport, boolean deleted) {
		super();
		this.id = id;
		this.ordreClasse = ordreClasse;
		this.numeroInscription = numeroInscription;
		this.dateInscription = dateInscription;
		this.nom = nom;
		this.prenom = prenom;
		this.dateNaissance = dateNaissance;
		this.lieuNaissance = lieuNaissance;
		this.adresse = adresse;
		this.telephoneParent = telephoneParent;
		this.emailParent = emailParent;
		this.emailEleve = emailEleve;
		this.paiementMensuel = paiementMensuel;
		this.paiementTransport = paiementTransport;
		this.deleted = deleted;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getOrdreClasse() {
		return ordreClasse;
	}

	public void setOrdreClasse(int ordreClasse) {
		this.ordreClasse = ordreClasse;
	}

	public GenreEleve getGenre() {
		return genre;
	}

	public void setGenre(GenreEleve genre) {
		this.genre = genre;
	}

	public String getNumeroInscription() {
		return numeroInscription;
	}

	public void setNumeroInscription(String numeroInscription) {
		this.numeroInscription = numeroInscription;
	}

	public String getNumeroInscriptionNational() {
		return numeroInscriptionNational;
	}

	public void setNumeroInscriptionNational(String numeroInscriptionNational) {
		this.numeroInscriptionNational = numeroInscriptionNational;
	}

	public Date getDateInscription() {
		return dateInscription;
	}

	public void setDateInscription(Date dateInscription) {
		this.dateInscription = dateInscription;
	}

	public String getFullName(){
		return (prenom != null ? prenom : "") + " " + (nom != null ? nom : "");
	}
	
	public String getFullNameAr(){
		return (prenomAr != null ? prenomAr : "") + " " + (nomAr != null ? nomAr : "");
	}
	
	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getImageToDisplay() {
		imageToDisplay = CommonUtils.getImageFromFile(image);
		return imageToDisplay;
	}

	public void setImageToDisplay(String imageToDisplay) {
		this.imageToDisplay = imageToDisplay;
	}
	
	public Date getDateNaissance() {
		return dateNaissance;
	}

	public void setDateNaissance(Date dateNaissance) {
		this.dateNaissance = dateNaissance;
	}

	public String getLieuNaissance() {
		return lieuNaissance;
	}

	public void setLieuNaissance(String lieuNaissance) {
		this.lieuNaissance = lieuNaissance;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public String getTelephoneParent() {
		return telephoneParent;
	}

	public void setTelephoneParent(String telephoneParent) {
		this.telephoneParent = telephoneParent;
	}

	public String getTelephoneMere1() {
		return telephoneMere1;
	}

	public void setTelephoneMere1(String telephoneMere1) {
		this.telephoneMere1 = telephoneMere1;
	}

	public String getTelephoneMere2() {
		return telephoneMere2;
	}

	public void setTelephoneMere2(String telephoneMere2) {
		this.telephoneMere2 = telephoneMere2;
	}

	public String getTelephonePere1() {
		return telephonePere1;
	}

	public void setTelephonePere1(String telephonePere1) {
		this.telephonePere1 = telephonePere1;
	}

	public String getTelephonePere2() {
		return telephonePere2;
	}

	public void setTelephonePere2(String telephonePere2) {
		this.telephonePere2 = telephonePere2;
	}

	public String getEmailParent() {
		return emailParent;
	}

	public void setEmailParent(String emailParent) {
		this.emailParent = emailParent;
	}

	public String getEmailEleve() {
		return emailEleve;
	}

	public void setEmailEleve(String emailEleve) {
		this.emailEleve = emailEleve;
	}

	public float getPaiementMensuel() {
		return paiementMensuel;
	}

	public void setPaiementMensuel(float paiementMensuel) {
		this.paiementMensuel = paiementMensuel;
	}

	public float getPaiementTransport() {
		return paiementTransport;
	}

	public void setPaiementTransport(float paiementTransport) {
		this.paiementTransport = paiementTransport;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public Groupe getGroupe() {
		return groupe;
	}

	public void setGroupe(Groupe groupe) {
		this.groupe = groupe;
	}

	public String getNomAr() {
		return nomAr;
	}

	public void setNomAr(String nomAr) {
		this.nomAr = nomAr;
	}

	public String getPrenomAr() {
		return prenomAr;
	}

	public void setPrenomAr(String prenomAr) {
		this.prenomAr = prenomAr;
	}

	public String getLieuNaissanceAr() {
		return lieuNaissanceAr;
	}

	public void setLieuNaissanceAr(String lieuNaissanceAr) {
		this.lieuNaissanceAr = lieuNaissanceAr;
	}

	public String getAdresseAr() {
		return adresseAr;
	}

	public void setAdresseAr(String adresseAr) {
		this.adresseAr = adresseAr;
	}

	public Date getDateDepart() {
		return dateDepart;
	}

	public void setDateDepart(Date dateDepart) {
		this.dateDepart = dateDepart;
	}
	
	public List<MoisPaye> getMoisAPayes() {
		return moisAPayes;
	}

	public void setMoisAPayes(List<MoisPaye> moisAPayes) {
		this.moisAPayes = moisAPayes;
	}
	
	public String getMois() {
		StringBuilder builder = new StringBuilder();
		if(moisAPayes != null){
			for (MoisPaye moisPaye : moisAPayes) {
				builder.append(moisPaye.getLibelleAr() + ", ");
			}
		}
		
		return builder.toString();
	}

	public boolean isExonere() {
		return exonere;
	}

	public void setExonere(boolean exonere) {
		this.exonere = exonere;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Eleve other = (Eleve) obj;
		
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((dateInscription == null) ? 0 : dateInscription.hashCode());
		result = prime * result
				+ ((dateNaissance == null) ? 0 : dateNaissance.hashCode());
		result = prime * result + (deleted ? 1231 : 1237);
		result = prime * result
				+ ((emailEleve == null) ? 0 : emailEleve.hashCode());
		result = prime * result
				+ ((emailParent == null) ? 0 : emailParent.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((nom == null) ? 0 : nom.hashCode());
		result = prime
				* result
				+ ((numeroInscription == null) ? 0 : numeroInscription
						.hashCode());
		result = prime * result + ((prenom == null) ? 0 : prenom.hashCode());
		result = prime * result + Float.floatToIntBits(paiementMensuel);
		result = prime * result + Float.floatToIntBits(paiementTransport);
		result = prime * result
				+ ((telephoneParent == null) ? 0 : telephoneParent.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "Eleve [id=" + id + ", ordreClasse=" + ordreClasse
				+ ", numeroInscription=" + numeroInscription
				+ ", dateInscription=" + dateInscription + ", nom=" + nom
				+ ", prenom=" + prenom + ", dateNaissance=" + dateNaissance
				+ ", telephoneParent=" + telephoneParent + ", emailParent="
				+ emailParent + ", emailEleve=" + emailEleve
				+ ", paiementMensuel=" + paiementMensuel + " , paiementTransport=" + paiementTransport + ", deleted=" + deleted
				+ ", lieuNaissance=" + lieuNaissance + ", adresse=" + adresse
				+ "]";
	}

}
